<?php

namespace app\club\controller;

use app\common\controller\Clubend;
use think\Validate;
use think\Db;
use app\club\model\YAgency;
use think\Session;
use think\Config;

/**
 * 后台首页
 * @internal
 */
class Index extends Clubend
{

    protected $noNeedLogin = ['login','loginauth'];
    protected $noNeedRight = ['index', 'logout'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        $uid = $this->auth->uid;
        $AgeType = $this->auth->AgeType;

        $group_list = Db::table('fa_c_group')->where(['id'=>$AgeType])->find();
        if(empty($group_list)){
            $this->error('没有可访问的菜单');
        }
        $rule_list = Db::table('fa_c_rule')->where(['id'=>array('in',$group_list['rules']),'ismenu'=>1,'pid'=>0])->select();
        if(empty($rule_list)){
            $this->error('没有可访问的菜单');
        }
        
        $menulist = '';
        foreach ($rule_list as $key=>$val){
            $menulist = $menulist.'
            <li class="">
            <a href="/club/'.$val['name'].'" addtabs="'.$val['id'].'" url="/club/'.$val['name'].'">
            <i class="'.$val['icon'].'"></i>
            <span>'.$val['title'].'</span>
            <span class="pull-right-container"> </span>
            </a>
            </li>';
        }
        
        $this->view->assign('menulist', $menulist);
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }
    
    /**
     * 自动登录
     */
    public function loginauth(){
        $id = $this->request->get('id');
        $sign = $this->request->get('sign');
        $url = $this->request->get('url', 'index/index');
        if(empty($id) || empty($sign)){
            $this->redirect('index/login');
        }
        $my_sign = strtoupper(md5($id.Config::get('logkey.key')));
        if($sign !== $my_sign){
            $this->redirect('index/login');
        }
        
        $owner = YAgency::get(['ID'=>$id]);
        if(empty($owner)){
            $this->redirect('index/login');
        }else{
            Session::set("owner", $owner);
            $this->redirect($url);
        }
    }


    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        if ($this->request->isPost())
        {
            $LoginName = $this->request->post('username');
            $Password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $rule = [
                'LoginName'  => 'require|length:3,30',
                'Password'  => 'require|length:3,30',
                '__token__' => 'token',
            ];
            $data = [
                'LoginName'  => $LoginName,
                'Password'  => $Password,
                '__token__' => $token,
            ];
            $validate = new Validate($rule);
            $result = $validate->check($data);
            if (!$result)
            {
                $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
            }
            $result = $this->auth->login($LoginName, $Password, $keeplogin ? 86400 : 0);
            if ($result === true)
            {
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->uid, 'username' => $LoginName]);
            }
            else
            {
                $this->error(__('Username or password is incorrect'), $url, ['token' => $this->request->token()]);
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin())
        {
            $this->redirect($url);
        }
        $background = cdnurl("/assets/img/loginbg.jpg");
        $this->view->assign('background', $background);
        \think\Hook::listen("login_init", $this->request);
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'), 'index/login');
    }

}
